﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi.DataTable
{
    /// <summary>
    /// 获取单条数据对象
    /// </summary>
    [CPPEI.WebApi.AuthGetAction("DataTable", "GetSingleData", "1.0")]
    public class GetSingleDataAction : BaseAction<GetSingleDataParams, GetSingleDataResult>
    {
        protected override ActionResult<GetSingleDataResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            GetSingleDataParams opParams)
        {
            string[] pkColumnArr = opParams.PKColumns.Split(new char[]{','});
            string[] pkValueArr = opParams.PKValues.Split(new char[]{','});
            SER.DtTableService service = new SER.DtTableService();

            int total = 0;
            ServiceResult<CPPEI.ETL.Domain.Models.MDtTableData> serResult = service.GetSingleData(opParams.TableId, pkColumnArr, pkValueArr);
            if (serResult.IsSuccess)
            {
                GetSingleDataResult actResult = new GetSingleDataResult();
                
                List<string> columns = new List<string>();
                List<string> types = new List<string>();
                for (int colIndex = 0; colIndex < serResult.Data.DataTable.Columns.Count; colIndex++)
                {
                    columns.Add(serResult.Data.DataTable.Columns[colIndex].ColumnName);
                    types.Add(serResult.Data.DataTable.Columns[colIndex].DataType.FullName);
                }
                actResult.Types = types;
                actResult.Columns = columns;
                actResult.Types = types;

                actResult.PKColumns = new List<string>();
                for (int pkIndex = 0; pkIndex < serResult.Data.PKCloumns.Count; pkIndex++)
                {
                    actResult.PKColumns.Add(serResult.Data.PKCloumns[pkIndex]);
                }
                
                for (int rowIndex = 0; rowIndex < serResult.Data.DataTable.Rows.Count; rowIndex++)
                {
                    System.Data.DataRow dataRow = serResult.Data.DataTable.Rows[rowIndex];
                    GetSingleDataResult.Item item = new GetSingleDataResult.Item();

                    for (int colIndex = 0; colIndex < actResult.Columns.Count; colIndex++)
                    {
                        item.Add(Convert.ToString(dataRow[actResult.Columns[colIndex]]));
                    }

                    actResult.Data = item;
                    break;
                }

                return new ActionResult<GetSingleDataResult>(actResult);
            }
            else
            {
                return new ActionResult<GetSingleDataResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetSingleDataParams
    {
        public Guid TableId { get; set; }

        /// <summary>
        /// 数据主键列信息（pk1,pk2,pk3）,顺序要和值的顺序一样
        /// </summary>
        public string PKColumns { get; set; }

        /// <summary>
        /// 数据主键的值信息（pkvalue1,pkvalue2,pkvalue3),顺序要和名的顺序一样
        /// </summary>
        public string PKValues { get; set; }
    }

    public class GetSingleDataResult
    {
        public List<string> Columns { get; set; }

        public List<string> PKColumns { get; set; }

        public List<string> Types { get; set; }

        public Item Data { get; set; }

        public class Item
        {
            public List<string> Values { get; set; }

            public Item()
            {
                Values = new List<string>();
            }

            public void Add(string value)
            {
                Values.Add(value);
            }
        }
    }
}
